#include "save.h"

extern XI_IMG image;


Save::Save(QObject *parent) : QThread(parent)
{
    connect(this,SIGNAL(proSignal()),this,SLOT(proSlot()));
}


void Save::run()
{
    int num = 0;
    unsigned short *image_bp = ((unsigned short *)image.bp);
    image_bp += IMAGE_WIDTH * OFFSET_Y;

    for(int i=0; i<FILTER_HEIGHT/PATTERN_WIDTH; i++)//waves
    {
        for(int j=0; j<FILTER_WIDTH/PATTERN_WIDTH; j++)
        {
            waveBand01[num] = *(image_bp+IMAGE_WIDTH*0+0);
            waveBand02[num] = *(image_bp+IMAGE_WIDTH*0+1);
            waveBand03[num] = *(image_bp+IMAGE_WIDTH*0+2);
            waveBand04[num] = *(image_bp+IMAGE_WIDTH*0+3);
            waveBand05[num] = *(image_bp+IMAGE_WIDTH*0+4);
            waveBand06[num] = *(image_bp+IMAGE_WIDTH*1+0);
            waveBand07[num] = *(image_bp+IMAGE_WIDTH*1+1);
            waveBand08[num] = *(image_bp+IMAGE_WIDTH*1+2);
            waveBand09[num] = *(image_bp+IMAGE_WIDTH*1+3);
            waveBand10[num] = *(image_bp+IMAGE_WIDTH*1+4);
            waveBand11[num] = *(image_bp+IMAGE_WIDTH*2+0);
            waveBand12[num] = *(image_bp+IMAGE_WIDTH*2+1);
            waveBand13[num] = *(image_bp+IMAGE_WIDTH*2+2);
            waveBand14[num] = *(image_bp+IMAGE_WIDTH*2+3);
            waveBand15[num] = *(image_bp+IMAGE_WIDTH*2+4);
            waveBand16[num] = *(image_bp+IMAGE_WIDTH*3+0);
            waveBand17[num] = *(image_bp+IMAGE_WIDTH*3+1);
            waveBand18[num] = *(image_bp+IMAGE_WIDTH*3+2);
            waveBand19[num] = *(image_bp+IMAGE_WIDTH*3+3);
            waveBand20[num] = *(image_bp+IMAGE_WIDTH*3+4);
            waveBand21[num] = *(image_bp+IMAGE_WIDTH*4+0);
            waveBand22[num] = *(image_bp+IMAGE_WIDTH*4+1);
            waveBand23[num] = *(image_bp+IMAGE_WIDTH*4+2);
            waveBand24[num] = *(image_bp+IMAGE_WIDTH*4+3);
            waveBand25[num] = *(image_bp+IMAGE_WIDTH*4+4);
            num++;
            image_bp+=PATTERN_WIDTH;
        }
        image_bp+=3;
        image_bp+=IMAGE_WIDTH*(PATTERN_WIDTH-1);
    }


    GDALAllRegister();

    GDALDataset *poDstDS;
    GDALDriver  *poDriver;
    GDALRasterBand *poBand;
    CPLErr err;
    char **papszOptions = NULL;

    poDriver = GetGDALDriverManager()->GetDriverByName("Gtiff");
    poDstDS = poDriver->Create( "newgeo.tif", FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH, 25, GDT_UInt16,
                                papszOptions );

    poBand = poDstDS->GetRasterBand(1);
    err = poBand->RasterIO( GF_Write, 0, 0, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH,
                      (unsigned short*)waveBand01, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH, GDT_UInt16, 0, 0 );
    usleep(10000);
    poBand = poDstDS->GetRasterBand(2);
    err = poBand->RasterIO( GF_Write, 0, 0, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH,
                      (unsigned short*)waveBand02, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH, GDT_UInt16, 0, 0 );
    usleep(10000);
    poBand = poDstDS->GetRasterBand(3);
    err = poBand->RasterIO( GF_Write, 0, 0, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH,
                      (unsigned short*)waveBand03, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH, GDT_UInt16, 0, 0 );
    usleep(10000);
    poBand = poDstDS->GetRasterBand(4);
    err = poBand->RasterIO( GF_Write, 0, 0, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH,
                      (unsigned short*)waveBand04, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH, GDT_UInt16, 0, 0 );
    usleep(10000);
    poBand = poDstDS->GetRasterBand(5);
    err = poBand->RasterIO( GF_Write, 0, 0, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH,
                      (unsigned short*)waveBand05, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH, GDT_UInt16, 0, 0 );
    usleep(10000);
    poBand = poDstDS->GetRasterBand(6);
    err = poBand->RasterIO( GF_Write, 0, 0, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH,
                      (unsigned short*)waveBand06, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH, GDT_UInt16, 0, 0 );
    usleep(10000);
    poBand = poDstDS->GetRasterBand(7);
    err = poBand->RasterIO( GF_Write, 0, 0, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH,
                      (unsigned short*)waveBand07, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH, GDT_UInt16, 0, 0 );
    usleep(10000);
    poBand = poDstDS->GetRasterBand(8);
    err = poBand->RasterIO( GF_Write, 0, 0, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH,
                      (unsigned short*)waveBand08, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH, GDT_UInt16, 0, 0 );
    usleep(10000);
    poBand = poDstDS->GetRasterBand(9);
    err = poBand->RasterIO( GF_Write, 0, 0, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH,
                      (unsigned short*)waveBand09, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH, GDT_UInt16, 0, 0 );
    usleep(10000);
    poBand = poDstDS->GetRasterBand(10);
    err = poBand->RasterIO( GF_Write, 0, 0, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH,
                      (unsigned short*)waveBand10, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH, GDT_UInt16, 0, 0 );
    usleep(10000);
    poBand = poDstDS->GetRasterBand(11);
    err = poBand->RasterIO( GF_Write, 0, 0, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH,
                      (unsigned short*)waveBand11, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH, GDT_UInt16, 0, 0 );
    usleep(10000);
    poBand = poDstDS->GetRasterBand(12);
    err = poBand->RasterIO( GF_Write, 0, 0, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH,
                      (unsigned short*)waveBand12, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH, GDT_UInt16, 0, 0 );
    usleep(10000);
    poBand = poDstDS->GetRasterBand(13);
    err = poBand->RasterIO( GF_Write, 0, 0, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH,
                      (unsigned short*)waveBand13, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH, GDT_UInt16, 0, 0 );
    usleep(10000);
    poBand = poDstDS->GetRasterBand(14);
    err = poBand->RasterIO( GF_Write, 0, 0, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH,
                      (unsigned short*)waveBand14, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH, GDT_UInt16, 0, 0 );
    usleep(10000);
    poBand = poDstDS->GetRasterBand(15);
    err = poBand->RasterIO( GF_Write, 0, 0, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH,
                      (unsigned short*)waveBand15, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH, GDT_UInt16, 0, 0 );
    usleep(10000);
    poBand = poDstDS->GetRasterBand(16);
    err = poBand->RasterIO( GF_Write, 0, 0, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH,
                      (unsigned short*)waveBand16, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH, GDT_UInt16, 0, 0 );
    usleep(10000);
    poBand = poDstDS->GetRasterBand(17);
    err = poBand->RasterIO( GF_Write, 0, 0, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH,
                      (unsigned short*)waveBand17, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH, GDT_UInt16, 0, 0 );
    usleep(10000);
    poBand = poDstDS->GetRasterBand(18);
    err = poBand->RasterIO( GF_Write, 0, 0, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH,
                      (unsigned short*)waveBand18, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH, GDT_UInt16, 0, 0 );
    usleep(10000);
    poBand = poDstDS->GetRasterBand(19);
    err = poBand->RasterIO( GF_Write, 0, 0, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH,
                      (unsigned short*)waveBand19, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH, GDT_UInt16, 0, 0 );
    usleep(10000);
    poBand = poDstDS->GetRasterBand(20);
    err = poBand->RasterIO( GF_Write, 0, 0, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH,
                      (unsigned short*)waveBand20, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH, GDT_UInt16, 0, 0 );
    usleep(10000);
    poBand = poDstDS->GetRasterBand(21);
    err = poBand->RasterIO( GF_Write, 0, 0, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH,
                      (unsigned short*)waveBand21, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH, GDT_UInt16, 0, 0 );
    usleep(10000);
    poBand = poDstDS->GetRasterBand(22);
    err = poBand->RasterIO( GF_Write, 0, 0, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH,
                      (unsigned short*)waveBand22, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH, GDT_UInt16, 0, 0 );
    usleep(10000);
    poBand = poDstDS->GetRasterBand(23);
    err = poBand->RasterIO( GF_Write, 0, 0, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH,
                      (unsigned short*)waveBand23, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH, GDT_UInt16, 0, 0 );
    usleep(10000);
    poBand = poDstDS->GetRasterBand(24);
    err = poBand->RasterIO( GF_Write, 0, 0, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH,
                      (unsigned short*)waveBand24, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH, GDT_UInt16, 0, 0 );
    usleep(10000);
    poBand = poDstDS->GetRasterBand(25);
    err = poBand->RasterIO( GF_Write, 0, 0, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH,
                      (unsigned short*)waveBand25, FILTER_WIDTH/PATTERN_WIDTH, FILTER_HEIGHT/PATTERN_WIDTH, GDT_UInt16, 0, 0 );
    usleep(10000);
    poDstDS->FlushCache();
    GDALClose( (GDALDatasetH) poDstDS );

    emit proSignal();
}




void Save::proSlot()
{
    pro = new QProcess;
    connect(pro,SIGNAL(finished(int)),this,SLOT(proFinishSlot(int)));
    QString program = "cp";
    QStringList argument;

    argument<<"newgeo.tif";
    argument<< "/media/nvidia/SD22/"; // /home/nvidia/Pictures/
    pro->start(program,argument);
}

void Save::proFinishSlot(int exitCode)
{
    exitCode = exitCode;
    QString time;
    QDateTime current_date_time = QDateTime::currentDateTime();
    time = current_date_time.toString("yyyy_MM_dd_hh_mm_ss_zzz");
    QString buf = "/media/nvidia/SD22/"; // /media/root/X/
    buf.append(time);
    buf.append(".tif");
    delete pro;

    proCp = new QProcess;
    connect(proCp,SIGNAL(finished(int)),this,SLOT(proCpFinishSlot(int)));
    QString program = "mv";
    QStringList argument;

    argument<<"/media/nvidia/SD22/newgeo.tif";   // /media/root/X/newgeo.tif
    argument<<buf;
    proCp->start(program,argument);
}


void Save::proCpFinishSlot(int exitCode)
{
    exitCode = exitCode;
    delete proCp;
    emit saveokSignal();
}





